##12345############################

#Case Sandbox MichS

#Controlling Case

#Problem
#=======

#By  default, case-sensitivty  is in  effect when  awk performs  string  
#comparionsons.

#Solution
#========

#If you  need to  control the case  of a given  string, you  have three
#choices. The first  two work with most implementations of  awk as well
#as gawk.  The first method  uses the tolower() function  which returns
#all charecters supplied as a lowercase string.

#  result = tolower("FooBar")

#Now the string variable 'result' equals 'foobar'.

#Conversely,  the toupper()  function, will  return all  the characters
#supplied as an uppercase string:

#  RESULT = toupper("FooBar")

#Now the string variable 'RESULT' equals 'FOOBAR'.

#The third method  is specific to GNU  gawk, and is used  in a slightly
#different manner:

#  BEGIN{IGNORECASE = 1}

#In the example above, we're using the special gawk built-in IGNORECASE
#which  when set  to any  non-zero value,  tells gawk  to discard  case
#sensitivity  when performing  string comparisons  throughout your  awk
#program  file.  Typically  (but  not   always),  you'll  want  to  use
#IGNORECASE within the BEGIN{} block of your program file.

##12345############################

#accessingSubstrings
#Strings Sandbox MichS

#Accessing Substrings

#Problem
#=======

#How to (e.g.) extract characters 1  to 3 (abc) from the variable named
#buffer and store the result in a variable.

#Often you'll  find the need to  extract a part of  an existing string,
#into another string. awk provides  this functionally with the substr()
#function:

#  result = substr(var, start [, length])

#In  the expression  above, substr()  returns a  portion of  the string
#variable named  'var' beginning at  the position named 'start'  in the
#2nd argument, with  an optional 'length' for the 3rd  argument (if the
#length argument is not supplied,  substr() returns all characters from
#'start' till the end of the input string).

#If for  instance, you had a  string named 'buffer' with  the following
#characters:

#  buffer = "abcdef"

#and you needed to extract the  1st three characters, you would specify
#a 'start' position of 1 (character  indexes always start at 1),  and a
#'length' of 3 as shown in the example below.

#Solution
#========

#The following code returns _abc_.

#  buffer = "abcdef"
#  result = substr(buffer, 1, 3)

#Author
#======

#Michael Sanders

    function AccessingSubstrings(buffer, result) {
	buffer = "abcdef"
	result = substr(buffer, 1, 3)
	print result == "abc"
    }

##12345###########################

#takingStringsApart
#Strings Sandbox MichS

#How to Take Strings Apart

#Problem
#=======

#How to  find strings in larger strings.

#Solution
#========

#In the following,  'x' becomes 1, since the 1st character in the string 'str' is 'a'

 function takeApart1(      str,find,x) {
       str  = "abc"
       find = "a"
       x    = index(str, find)
       return x
 }

#In the following,  'x' becomes  2, since the 2nd character in the string 'str' is 'b'.

 function takeApart2(    str,find,x) {
	str  = "abc"
	find = "b"
	x    = index(str, find)
        return x
 }

#In the following, 'x' equals 3, since the 3rd character in the string 'str' is 'c'.

 function takeApart3(    str,find,x) {
       str  = "abc"
       find = "c"
       x    = index(str, find)
       return x
 }

#In the following, 'x' equals 0 (zero), since the string 'in' *does not* contain 'C'.

 function takeApart4(    str,find,x) {
	str  = "abc"
	find = "C"
	x    = index(str, find)
	return x
 }

#Author
#======

#Michael Sanders

   function TakeApart() {
	  print takeApart1() == 1
	  print takeApart2() == 2
	  print takeApart3() == 3
	  print takeApart4() == 0
   }

##12345#################################

#currentDateTime 
#DateTime Sandbox MichS

#Finding the Current Date and Time:

#Problem
#=======

#Accessing the current date and time

#Solution
#========

#Using the following function, we can access this date information:

#  the_date            : 03/06/2010
#  the_time            : 20:37:45
#  the_zone            : EST
#  the_minute          : 37
#  the_second          : 45
#  the_name_of_day     : Saturday
#  the_hour_twelve     : 08
#  the_hour_twentyfour : 20
#  the_ampm            : PM
#  sun_day_of_week     : 6
#  mon_day_of_week     : 6 
#  the_month           : 03
#  the_name_of_month   : March
#  the_day_of_month    : 06
#  the_year            : 2010
#  the_day_of_year     : 065
#  sun_week_of_year    : 09
#  mon_week_of_year    : 09

#Code 
#----

 function demoDateTime() {
      ##-------------------------------------------
      # basic
      print "the_date            : " strftime("%x") 
      print "the_time            : " strftime("%X") 
      print "the_zone            : " strftime("%Z") 
	  #
      ##-------------------------------------------
      ## minutes/secs
      # minute as decimal (00-59)
      print "the_minute          : " strftime("%M") 
      # second of minute as decimal (00-60)
      print "the_second          : " strftime("%S") 
      # 
      ##-------------------------------------------
      ## day/hour
      print "the_name_of_day     : " strftime("%A") 
      print "the_hour_twelve     : " strftime("%I") 
      print "the_hour_twentyfour : " strftime("%H") 
      # locale's equivalent of AM/PM
      print "the_ampm            : " strftime("%p") 
      #
      ##-------------------------------------------
      ## by week
      # weekday as decimal (0-6) starts with Sunday
      print "sun_day_of_week     : " strftime("%w") 
      # weekday as decimal (1-7) starts with Monday
      print "mon_day_of_week     : " strftime("%u") 
      #
      ##-------------------------------------------
      ## by month
      # month as decimal (01-12)
      print "the_month           : " strftime("%m") 
      # locale's full month nam
      print "the_name_of_month   : " strftime("%B") 
      # day of the month as decimal (01-31)
      print "the_day_of_month    : " strftime("%d") 
      #
      ##-------------------------------------------
      ## by year
      # year as decimal (2010)
      print "the_year            : " strftime("%G") 
      # day of the year as decimal (001-366)
      print "the_day_of_year     : " strftime("%j") 
      # week of year (00-53) starts with Sunday
      print "sun_week_of_year    : " strftime("%U") 
      # week of year (01-53) starts with Monday
      print "mon_week_of_year    : " strftime("%V") 
 }

#Author
#======

#Michael Sanders

##12345 #############################

#isNum
#Numbers MikeS 

#Checking Whether a variable Contains a Valid Number

#Problem
#=======

#How to peek at a variable, to check if it is a number.

#Solution
#========

#_isNum_ returns a positive value if string is numeric

 function isNum(x) {
   return(x == x + 0)
 }

#Usage
#=====

#  if (! isNum(var)) {print "not a number"}

   function IsNum (  x) {
       x=23     ;print "isNum " x "? " isNum(x)
       x="a a " ;print "isNum " x "? " isNum(x)
   }

#Discussion
#==========

#Two related problems are checking if [?isnumString a string contains a valid number] and
#[?string2num converting between a string and a number].

#Author
#======

#Michael Sanders

##12345 #############################

#rInt
#Numbers Random MikeS 

#Generating Random Integers Within a Range

#Problem
#=======

#How to generate a random integer between two ranges.

#Solution
#========

 function rInt(low, hi,    x, y, z) {
     seed()
     x = rand()
     y = (hi - low) + 1
     z = int((x * y) + low)
     return z
}

#Author
#======

#Michael Sanders

##12345 #############################

#rFloat
#Numbers Random MikeS 

#Generating Random Floats Within a Range

#Problems
#=======

#How to generate a random float between two ranges.

#Solution
#========

#Use seed.

 function rFloat(low, hi,    x, y, z) {
     seed()
     x = rand()
     y = hi - low
     z = (x * y) + low
     return z
}

#Author
#======

#Michael Sanders

